package com.jtny.nytb.dataparam.service.serviceImpl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jtny.component.core.util.PageUtils;
import com.jtny.component.entity.PageData;
import com.jtny.component.util.BeanUtils;
import com.jtny.nytb.config.coderule.dto.ConfigRuleSequenceNumberDTO;
import com.jtny.nytb.config.coderule.dto.ConfigRuleSequenceNumberPageDTO;
import com.jtny.nytb.dataparam.service.ConfigRuleSequenceNumberService;
import com.jtny.nytb.equipment.dao.coderule.ConfigRuleSequenceNumberDAO;
import com.jtny.nytb.equipment.domain.coderule.ConfigRuleSequenceNumberDO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

@Service
public class ConfigRuleSequenceNumberServiceImpl implements ConfigRuleSequenceNumberService {

    @Autowired
    private ConfigRuleSequenceNumberDAO configRuleSequenceNumberDAO;

    /**
     * 保存顺序号
     * @param configRuleSequenceNumberDTO
     * @return
     */
    @Override
    public ConfigRuleSequenceNumberDTO save(ConfigRuleSequenceNumberDTO configRuleSequenceNumberDTO) {
        ConfigRuleSequenceNumberDO configRuleSequenceNumberDO = BeanUtils.copyToObject(configRuleSequenceNumberDTO, ConfigRuleSequenceNumberDO.class);
        configRuleSequenceNumberDAO.insert(configRuleSequenceNumberDO);
        return configRuleSequenceNumberDTO;
    }

    @Override
    public PageData<ConfigRuleSequenceNumberDTO> queryPage(ConfigRuleSequenceNumberPageDTO configRuleSequenceNumberPageDTO) {
        QueryWrapper<ConfigRuleSequenceNumberDO> wrapper = new QueryWrapper<>();
        if (configRuleSequenceNumberPageDTO.getSequenceNumber() != null && configRuleSequenceNumberPageDTO.getSequenceNumber() != 0 ){
            wrapper.eq("sequence_number", configRuleSequenceNumberPageDTO.getSequenceNumber());
        }
        if (StringUtils.isNotBlank(configRuleSequenceNumberPageDTO.getLastcode())){
            wrapper.eq("lastcode", configRuleSequenceNumberPageDTO.getLastcode());
        }
        if (StringUtils.isNotBlank(configRuleSequenceNumberPageDTO.getSequenceSign())){
            wrapper.eq("sequence_sign", configRuleSequenceNumberPageDTO.getSequenceSign());
        }
        wrapper.orderByDesc("id");
        Page<ConfigRuleSequenceNumberDO> page = Page.of(configRuleSequenceNumberPageDTO.getPageNo(), configRuleSequenceNumberPageDTO.getPageSize());
        PageData<ConfigRuleSequenceNumberDO> pageDataForDO = PageUtils.getPageData(configRuleSequenceNumberDAO.selectPage(page, wrapper), configRuleSequenceNumberPageDTO);
        return pageDataForDO.transformData(ConfigRuleSequenceNumberDTO.class);
    }


    @Override
    public ConfigRuleSequenceNumberDTO querylastNumber(String lastcode, String sequenceSign) {
        QueryWrapper<ConfigRuleSequenceNumberDO> wrapper = new QueryWrapper<>();
        if (lastcode == null){
            wrapper.isNull("lastcode");
        }else{
            wrapper.eq("lastcode", lastcode);
        }
        wrapper.eq("sequence_sign", sequenceSign);
        wrapper.orderByDesc("create_time");
        List<ConfigRuleSequenceNumberDO> configRuleSequenceNumberDOList = configRuleSequenceNumberDAO.selectList(wrapper);
        ConfigRuleSequenceNumberDTO configRuleSequenceNumberDTO;
        if (CollectionUtil.isEmpty(configRuleSequenceNumberDOList)){
            configRuleSequenceNumberDTO = new ConfigRuleSequenceNumberDTO();
            configRuleSequenceNumberDTO.setSequenceNumber(1);
            configRuleSequenceNumberDTO.setStep(1);
            configRuleSequenceNumberDTO.setLastcode(lastcode);
            configRuleSequenceNumberDTO.setSequenceSign(sequenceSign);
            ConfigRuleSequenceNumberDO configRuleSequenceNumberDO = new ConfigRuleSequenceNumberDO();
            configRuleSequenceNumberDO.setLastcode(configRuleSequenceNumberDTO.getLastcode());
            configRuleSequenceNumberDO.setSequenceSign(sequenceSign);
            configRuleSequenceNumberDO.setSequenceNumber(configRuleSequenceNumberDTO.getSequenceNumber());
            configRuleSequenceNumberDO.setStep(configRuleSequenceNumberDTO.getStep());
            configRuleSequenceNumberDO.setCreateTime(System.currentTimeMillis());
            configRuleSequenceNumberDO.setLastUpdateTime(System.currentTimeMillis());
            configRuleSequenceNumberDAO.insert(configRuleSequenceNumberDO);
        }else{
            configRuleSequenceNumberDTO = BeanUtils.copyToObject(configRuleSequenceNumberDOList.get(0), ConfigRuleSequenceNumberDTO.class);
        }
        ConfigRuleSequenceNumberDO configRuleSequenceNumberDO = new ConfigRuleSequenceNumberDO();
        configRuleSequenceNumberDO.setLastcode(configRuleSequenceNumberDTO.getLastcode());
        configRuleSequenceNumberDO.setSequenceSign(sequenceSign);
        configRuleSequenceNumberDO.setSequenceNumber(configRuleSequenceNumberDTO.getSequenceNumber() + configRuleSequenceNumberDTO.getStep());
        configRuleSequenceNumberDO.setStep(configRuleSequenceNumberDTO.getStep());
        configRuleSequenceNumberDO.setCreateTime(System.currentTimeMillis());
        configRuleSequenceNumberDO.setLastUpdateTime(System.currentTimeMillis());
        configRuleSequenceNumberDAO.insert(configRuleSequenceNumberDO);
        return configRuleSequenceNumberDTO;
    }
}
